package com.lagou.rpc.provider.zookeeper;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ZookeeperServer {
    private static String connectString = "192.168.200.20:2181";
    private static int sessionTimeout = 60000;
    private ZooKeeper zk = null;

    // 创建到zk的客户端连接
    public void getConnect() throws IOException {
        zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
            }
        });
    }

    // 注册到集群
    public void register(String serverName) throws Exception {
        // 一定是"EPHEMERAL_SEQUENTIAL短暂有序型"的节点，才能给shop编号，shop1，shop2...”
        String create = zk.create("/lg-rpc-provider/" + serverName, "0".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println("【" + serverName + "】 开始上线！ " + create);
    }

    // 业务功能
    public void business(String serverName) throws Exception {
        System.out.println("【" + serverName + "】 正在提供服务中 ...");
        System.in.read();
    }
}
